import { Vec2 } from 'cc';
import { BezierUtils } from './Utils/BezierUtils';
import { PathPoint } from './Utils/PathPoint';

//路径管理器
export class PathManager {
    
    private paths: PathPoint[] = [];
    private currentPathIndex: number = 0;
    private currentT: number = 0;

    constructor(paths: PathPoint[]) {
        this.paths = paths;
    }

    public getPaths(): PathPoint[] {
        return this.paths;
    }

    public updatePosition(deltaTime: number): Vec2 | null {
        // 检查是否完成所有路径
        if (this.currentPathIndex >= this.paths.length) {
            return null;
        }

        const currentPath = this.paths[this.currentPathIndex];

        // 计算当前路径段的位置
        const point = BezierUtils.getBezierPoint(
            this.currentT,
            new Vec2(currentPath.startPos[0],currentPath.startPos[1]),
            new Vec2(currentPath.controlPos[0],currentPath.controlPos[1]),
            new Vec2(currentPath.endPos[0],currentPath.endPos[1])
        );
        

        // 更新进度
        this.currentT += deltaTime * currentPath.speed;

        // 如果当前路径段完成，切换到下一段
        if (this.currentT >= 1) {
            this.currentPathIndex++;
            this.currentT = 0;
        }

        return point;
    }

    public isCompleted(): boolean {
        return this.currentPathIndex >= this.paths.length;
    }

    //返回当前的转身状态
    public getCurrTurnState(){
        // 检查是否完成所有路径
        if (this.currentPathIndex < this.paths.length) {
            return this.paths[this.currentPathIndex].turnState;
        }
        return null;
    }
}